package test;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import com.mostka.serializer.java.Serializer;
import com.mostka.serializer.java.SerializerInt;

public class Main {
	public static void main(String arf[]){
		System.out.println(0xFF);
		System.out.println("start");
		
		SerializerInt ser = new Serializer();
		
		String testStr = "superTest";
		int testInt = 555555555;
		int negativeInt = -1;
		int[] testIntArr = new int[]{testInt,testInt,testInt,testInt};
		short testShort = 15645;
		short[] testShortArr = new short[]{testShort,testShort,testShort,testShort};
		long testlong = Long.MAX_VALUE-10;
		long[] testlongArr = new long[]{testlong,testlong,testlong,testlong};
		double testDouble = 12345.6789;
		double[] testSoubleArr = new double[]{testDouble,testDouble,testDouble,testDouble};

		ser.writeValue(testInt);
		ser.writeValue(negativeInt);
		ser.writeValue(testStr);
		ser.writeValue(testShort);
		ser.writeValue(testlong);
		ser.writeValue(testDouble);

		ser.writeValue(testIntArr);
		ser.writeValue(testShortArr);
		ser.writeValue(testlongArr);
		ser.writeValue(testSoubleArr);
		
		TestObject to = new TestObject();
		TestObjectGenerated.___serialize(ser, to);
		
		

		String buffer = ser.getBuffer();
		System.out.println("middle");
		System.out.println(buffer);
		try {
			assert_(Arrays.equals(buffer.getBytes("ISO-8859-1"),buffer.getBytes("ISO-8859-1")), "bytes to bytes conversation failed");
			assert_(Arrays.equals(buffer.getBytes("ISO-8859-1"),ser.getByteBuffer()), "bytes to string conversation failed");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		
		SerializerInt des = new Serializer(buffer);
		
		try {
			int i = des.readInteger();
			assert_(i==testInt,"testInt:"+i);
			i = des.readInteger();
			assert_(i==negativeInt,"testInt:"+i);
			String a = des.readString();
			assert_(a.equals(testStr),testStr);
			assert_(des.readShort()==testShort,"testShort");
			long l = des.readLong();
			assert_(l==testlong,"testlong:"+l);
			assert_(des.readDouble()==testDouble,"testDouble");

			assert_(Arrays.equals(des.readIntegerArr(), testIntArr),"testIntArr");
			assert_(Arrays.equals(des.readShortArr(), testShortArr),"testShortArr");
			assert_(Arrays.equals(des.readLongArr(), testlongArr),"testlongArr");
			assert_(Arrays.equals(des.readDoubleArr(), testSoubleArr),"testSoubleArr");
			
			TestObject so = TestObjectGenerated.___deserialize(des);
			assert_(to.equals(so),"TestObjectGenerated");
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("end");
		
		try {
			FileOutputStream output = new FileOutputStream(new File("output"));
			BufferedOutputStream bos = new BufferedOutputStream(output);
			bos.write(ser.getByteBuffer());
			bos.close();
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void assert_(boolean eq,String message){
		if (!eq){
			System.err.println("[Error] "+message);
		}else{
			System.out.println("[OK] "+message);
		}
	}
	
}
